package viz.view.mediators
{
	import flare.vis.data.NodeSprite;
	
	import flash.display.DisplayObject;
	
	import mx.core.Container;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;
	
	import viz.model.flare.data.StateItem;

	public class EditMediator extends Mediator
	{
		
		public static const NAME:String ="EditMediator";
		public static const EDIT_FORMS_SHOW:String ="editForms/show";
		public static const EDIT_FORMS_HIDE:String ="editForms/hide";
		public static const EDIT_FORMS_ATTACH_TO_DISPLAY:String ="editForms/attachToDisplay";
		public static const EDIT_FORMS_REMOVE_FROM_DISPLAY:String ="editForms/removeFromDisplay";
		public static const EDIT_FORMS_REMOVED:String ="editForms/removed"
		public static const EDIT_FORMS_INCOMING_DATA:String = "editForms/incomingData";
		public static const EDIT_FORMS_CHANGE_DATA:String = "editForms/changeData";

		public function EditMediator( view:Container )
		{
			super(NAME, view);
		}
		
		override public function listNotificationInterests():Array{
			return [ 	EDIT_FORMS_HIDE,
						EDIT_FORMS_SHOW,
						EDIT_FORMS_ATTACH_TO_DISPLAY,
						EDIT_FORMS_REMOVE_FROM_DISPLAY,
						EDIT_FORMS_INCOMING_DATA ]
		}
		
		override public function handleNotification(note:INotification):void{
			switch(note.getName()){
				case EDIT_FORMS_SHOW:
					show( note.getBody() );
					break;
				
				case EDIT_FORMS_HIDE:
					hide( );
					break;
				
				case EDIT_FORMS_ATTACH_TO_DISPLAY:
					attachToDisplay( DisplayObject( note.getBody() ) );
					break;
					
				case EDIT_FORMS_REMOVE_FROM_DISPLAY:
					removeFromDisplay( DisplayObject( note.getBody() ) );
					break;
					
				case EDIT_FORMS_INCOMING_DATA:
					changeData( note.getBody() );
					break;
				
			}
		}
		
		private function getState( data:Object ):StateItem{
			if(data is NodeSprite ){
				return StateItem( NodeSprite( data ).data );
			}else if( data is StateItem ){
				return StateItem( data );
			}else{
				return null;
			}
		}
		
		private function attachToDisplay( child:DisplayObject ):void{
			view.addChild( child );
		}
		
		private function removeFromDisplay( child:DisplayObject ):void{
			view.removeChild( child );
		}
		
		public function changeData( data:Object ):void{
			var state:StateItem = getState( data );
			if(facade.hasMediator(StateFormMediator.NAME)){
				sendChangeDataNote( state );
			}else{
				addStateForm( state );
			}
		}
				
		public function show( data:Object ):void{
			
			addForms( getState( data ) );
		}
		
		private function addForms( state:StateItem ):void{
			addCreateStateForm( );
			addStateForm( state );
		};
		
		private function addCreateStateForm(  ):void{
			facade.registerMediator( new CreateStateFormMediator( )  )
		}
		private function addStateForm( state:StateItem ):void{
			if(!state)return;
			facade.registerMediator( new StateFormMediator( state )  )
		}
		
		private function sendRemovedNote():void{
			sendNotification(EDIT_FORMS_REMOVED);
		}
		
		private function sendChangeDataNote( state:StateItem ):void{
			sendNotification(EDIT_FORMS_CHANGE_DATA, state);
		}
		
		
		
		public function hide(  ):void{
			view.removeAllChildren();
			view.stage.focus = view.stage;
			sendRemovedNote();
		}
		
		public function get view():Container{
			return Container( viewComponent );
		}
		
	}
}